package com.beagle.njddapp.ui.presenter;

import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.StringBean;
import com.beagle.njddapp.bean.ZdBean;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.BaseZdHttpListResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.DeviceManagerView;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class DeviceManagerPresenter extends BasePresenter<DeviceManagerView> {
    private NjOwnerApiService apiService;

    public DeviceManagerPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        apiService = retrofit.create(NjOwnerApiService.class);

    }


    public void queryMeshNum(){

        apiService.queryMeshNum().compose(RxSchedulers.compose()).subscribe(new Observer<StringBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(StringBean stringBean) {
                if (stringBean!=null){
                    getView().showNumber(stringBean.getResult());
                }else {
                    getView().showNumber("");
                }
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");
            }

            @Override
            public void onComplete() {

            }

        });
    }
    public void getDeviceListRun(int size,int page){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("state","");
        paramsMap.put("latitude", "");
        paramsMap.put("longitude","");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.getDeviceList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<NjBean>>() {


            @Override
            public void success(BaseOilHttpListResponse<NjBean> baseOilHttpListResponse) {
                if(baseOilHttpListResponse!=null&&baseOilHttpListResponse.results!=null){
                    getView().showRunNj(baseOilHttpListResponse.results);
                }

            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);

            }
        });
    }
    public void deleteMeshBase( String meshId) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("meshId", meshId);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        apiService.deleteMeshBase(body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpResponse>() {


            @Override
            public void success(BaseOilHttpResponse baseOilHttpResponse) {
                LogUtils.d("success");

                    getView().showdelete();

            }

            @Override
            public void error(int code, String message) {
                LogUtils.d("error");
                getView().onError(message);
            }
        });

    }
}
